package pl.edu.agh.som.neighborhood;

import pl.edu.agh.som.Node;

public class SquareNeighborhood implements INeighborhoodFunction {

    int initialRadius;
    double radiusStep;

    public SquareNeighborhood(int initialRadius, int learningSteps){
        if (initialRadius < 1) {
            throw new RuntimeException("Must be: initialRadius >= 1");
        }

        this.initialRadius = initialRadius;
        this.radiusStep = ((double)initialRadius)/learningSteps;
    }

    @Override
    public double evaluate(Node node, Node winner, int learningStep) {
        int currentRadius = (int)(this.initialRadius - learningStep*radiusStep);
        return taxiDistance(node, winner) <= currentRadius ? 1 : 0;
    }

    private int taxiDistance(Node node1, Node node2) {
        return Math.abs(node2.getRow() - node1.getRow()) + Math.abs(node2.getColumn() - node1.getColumn());
    }
}
